import TableLyout from "@/components/commonPublic/tableLyoutLong.vue";
import elBreadcrumb from "@/components/public/el-breadcrumb.vue"; //面包屑
import publicForm from "@/components/materialDataManage/fromCom.vue"; //模糊查询
import tree from "@/components/fieldTestManage/tree";
import TableData from "@/components/public/el-table-pagination"; //主页面列表
import CustomDialog from "@/components/fieldTestManage/fieldCommission/index.vue";

import formJs from "./regexDetail.js"; //新增弹框配置项
import {
    getUserCompanyTreeList, //左侧机构树接口
    getFieldtestCheckWitnessPageList, //主页面列表接口
    resettingWTDLetter,
    exportFieldtestCheckWitness
} from "@/api/request.js";
// import { statusType2 } from "@/utils/config"; //配置项
export default {
    name: "fieldCommission",
    components: {
        TableLyout,
        elBreadcrumb, //面包屑
        publicForm, //模糊查询
        tree, //左侧组织机构数
        TableData, //主页面列表
        CustomDialog,
    },
    data() {
        return {
            name: "fieldCommission", //按钮配置页面代号
            queryCriteriaData: {}, //分页
            btnObj: [], //按钮
            searchObj: formJs.searchObj, //搜索条件

            // 列表
            table_config: formJs.table_config,
            tableData: [], //列表数据列表
            // checked: true, //查看下级按钮
            loading: false, //列表数据加载loading
            getList: {
                fCompanyid: "", // 左侧选中组织机构id
                userId: "", // 查询待办或已办时传 列表页查询不传
                startDates: "", //取样日期开始时间
                endDates: "", //取样日期结束时间
                keyType: "", // 上方查询条件样品种类
                taskNumber: "", // 上方查询条件进场编号
                modelType: "2", // 取样：0-待办 1-已办;收样：3-待办 4-已办；查看：传2
                page: 1, // 当前页码
                pageSize: 20, // 每页显示条数
                timeDates: "",
                fProjectposition:"",
                taskNo:"",
            }, // 列表给后端传参
            // 列表

            // 弹框组件
            currentRow: null, //列表详情
            dataFid: null, //fid
            isShow: false, //是否显示弹框
            isShow1: false, //是否显示弹框
            statusNum: null, //点击那个按钮
            displaySwitching: 3, //非0、1取样&收样；0取样；1收样
            dialogTitle: null,
            // 弹框组件

            // 左侧机构树
            treelength: false, //左侧树显示隐藏
            showName: "未选择公司",
            treeData: [], //左侧树状图数据
            defaultProps: {
                children: "children",
                label: "fShortname",
            },
            checkData: [],
            treeText: {
                companyId: null,
                text: null,
                userId: null,
            },
            // 左侧机构树

            // 下拉框树
            selectTreeData: [],
            selectCheckData: [],
            selectDefaultProps: {
                children: "children",
                label: "fSampletypename",
            },
            // 下拉框树
            userInfo: {}, //登录用户
        };
    },

    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("GxBack-userInfo"));

        if (JSON.parse(sessionStorage.getItem("GxBack-sampleManage_treeCheck"))) {
            var id = JSON.parse(
                sessionStorage.getItem("GxBack-sampleManage_treeCheck")
            );
            this.checkData = [id.fCompanyid];
        }
        this.getDate();
        this.getTree();
        this.btnRole(this.name);

        this.table_config.operation.cols = [{
            prop: "fStatus",
            type: "primary",
            handleRow: this.handleRow,
        }, ];
        this.table_config.operation["specialClass"] = function (rowData) {
            var color = null;
            if (rowData == "待提交") {
                color = "red !important";
            } else if (rowData == "已提交") {
                color = "green !important";
            } else {
                color = "#409eff !important";
            }
            return color;
        };
    },
    methods: {
        btnRole(name) {
            if (this.userInfo.userId != "System") {
                var btnList = [];
                var ModuleList = JSON.parse(
                    sessionStorage.getItem("GxBack-ModuleList")
                );
                if (ModuleList.length > 0) {
                    ModuleList.map((item) => {
                        if (item.fEncode == name) {
                            btnList = item.modulebuttonList;
                        }
                    });
                    btnList.map((item) => {
                        item["disabled"] = false;
                    });
                    this.btnObj = btnList;
                }
            } else {
                this.btnObj = formJs.btnObj;
            }
        },
        // 左侧树结构
        getTree() {
            this.treeText = {
                companyId: this.userInfo.companyId,
                userId: this.userInfo.userId,
                // text: "4", // 0其他  1 砼拌合站 2水稳 3沥青
            };
            getUserCompanyTreeList(this.treeText)
                .then((res) => {
                    if (res.errcode == 0) {
                        var companyTree = res.data.userCompanyTreeList;
                        this.treeData = this.commonJs.tranListToTreeData(companyTree);
                        if (this.treeData[0].children == undefined) {
                            this.treelength = false; //左侧组织机构是否显示
                        } else {
                            this.treelength = true;
                        }

                        if (this.checkData[0]) {
                            this.showName = JSON.parse(
                                sessionStorage.getItem("GxBack-sampleManage_treeCheck")
                            ).fShortname;

                            this.getList.fCompanyid = this.checkData[0];
                        } else {
                            this.checkData[0] = this.treeData[0].fCompanyid;
                            this.showName = this.treeData[0].fShortname; //当前点击组织机构名称
                            this.getList.fCompanyid = this.treeData[0].fCompanyid; //当前点击组织机构ID
                        }
                        this.yj_searchForm(this.getList); // 调用主页面列表接口
                        // 列表
                    } else {
                        this.$message.error(res.msg);
                        // this.$message.error("获取组织机构失败");
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 点击左侧机构树
        handleNodeClick(data) {
            this.showName = data.fShortname; //顶部显示机构名
            this.getList.fCompanyid = data.fCompanyid;
            this.getList.page = 1;
            this.getList.pageSize = 20;
            this.yj_searchForm(this.getList);
        },

        // 获取列表数据
        yj_searchForm(data) {
            window.sessionStorage.setItem(
                "GxBackfield-TestManage_getLists",
                JSON.stringify(data)
            );
            var obj = {
                fCompanyid: data.fCompanyid, //左侧选中组织机构id
                userId: this.userInfo.userId, //查询待办或已办时传 列表页查询不传
                keyType: data.keyType, // 上方查询条件样品种类
                taskNumber: data.taskNumber, // 上方查询条件进场编号
                page: data.page, //当前页码
                pageSize: data.pageSize, //每页显示条数
                modelType: "2", // 取样：0-待办 1-已办;收样：3-待办 4-已办；查看：传2
                startDates: data.startDates, //进场开始时间
                endDates: data.endDates, //进场结束时间
                fProjectposition:data.fProjectposition
            };
            this.loading = true;
            var that = this;
            getFieldtestCheckWitnessPageList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                        that.tableData = res.data.pageInfo.list;
                        this.queryCriteriaData = res.data.pageInfo;
                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    this.$message.error("列表数据 接口出错", error.msg);
                    this.loading = false;
                });
        },
        // 按钮
        getBtnName(name, status) {
            this.statusNum = status;
            this.yj_edit(status)
        },
        yj_edit(num) {
            if (num == 7) { //查看
                if (this.dataFid != null) {
                    this.editText();
                    this.dialogTitle = "查看";
                } else {
                    this.$message.warning("请先选择需要查看的数据");
                }
            } else if (num == 2) { // 编辑
                if (this.dataFid != null) {
                    this.editText();
                    this.dialogTitle = "编辑";
                } else {
                    this.$message.warning("请先选择需要编辑的数据");
                }
            }
            else if (num == 5) { //台账导出
                this.exportFieldtestCheckWitness(this.getList)
                // window.open("http://1.14.103.201:8300/IOTManagement/api/export/exportFieldtestCheckWitness?fCompanyid=" + this.getList.fCompanyid + "&startDates=" + this.getList.startDates + "&endDates=" + this.getList.endDates + "&taskNumber=" + this.getList.taskNumber);
            } else if (num == 8) { //打印
                // this.$message.warning("打印功能暂未开放");
                if (this.dataFid != null) {

                        window.open("http://1.14.103.201:8300//IOTManagement/App/upload/getFieldtestCheckWitnessPDF?fId=" + this.dataFid);
                   
                } else {
                    this.$message.warning("请先选择需要打印的数据");
                }
            }
            else  if(num == 2){
                if (this.dataFid != null) {
                    if(this.currentRow.fSamplereportnostr==''){
                        // this.yj_return(this.currentRow);
    
                    }else {
                        this.$message.warning("当前数据不可退回");
                    }
                    
                } else {
                    this.$message.warning("请先选择需要退回的数据");
                }
            }
        },
        yj_return(row) {
            if (row.fId != "" && row.fId != null) {
           
                    this.$confirm("是否继续撤销本条数据", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    })
                        .then(() => {
                            this.loading = true;
                            resettingWTDLetter({
                                fId: row.fId,
                                userId: this.userInfo.userId,
                                userName: this.userInfo.realName,
                            })
                                .then((res) => {
                                    if (res.errcode == 0) {
                                        this.loading = false;
                                        this.yj_searchForm(this.getList);
                                        this.$message.success("退回成功");
                                    } else {
                                        this.$message.error("退回失败", res.msg);
                                        this.loading = false;
                                    }
                                })
                                .catch(() => {
                                    console.log("退回失败");
                                    this.loading = false;
                                });
                        })
                        .catch(() => {
                            this.$message("取消撤销");
                        });
              
            } else {
                return this.$message.error("请先选择需要撤销的数据");
            }
        },
         exportFieldtestCheckWitness(data){
          exportFieldtestCheckWitness(data)
            .then((res) => {

                const blob = new Blob([res], {
                    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                });
                const url = window.URL.createObjectURL(blob);
                window.open(url)
            })
         
        },
        // 查看
        editText() {
            this.isShow = true;
        },
        yj_viewForm(row, label) {
            this.currentRow = row;
            this.dataFid = row.fId;
           
            this.dialogTitle = "查看";
            if (label == "委托编号") {
                this.statusNum = 7;
                this.editText();
            }
        },
        // 点击当前行
        rowChange(val) {
            this.currentRow = val;
            this.dataFid = val.fId;
            return val;
        },

        //获取时间
        getDate() {
            if (JSON.parse(sessionStorage.getItem("GxBackfield-TestManage_getLists"))) {
                var data = JSON.parse(
                    sessionStorage.getItem("GxBackfield-TestManage_getLists")
                );
                this.getList = {
                    fCompanyid: data.fCompanyid, // 左侧选中组织机构id
                    startDates: data.startDates, // 开始时间
                    endDates: data.endDates, // 截至时间
                    page: 1, // 当前页码
                    pageSize: 20, // 每页显示条数
                    userId: this.userInfo.userId,
                    modelType: "2",
                    taskNumber: data.taskNumber, // 上方查询条件进场编号
                    keyType: '', // 上方查询条件样品种类
                    timeDates: [data.startDates, data.endDates],
                    fProjectposition:data.fProjectposition,
                    taskNo:""

                };
                this.value2 = [this.getList.startDates, this.getList.endDates];
            } else {
                this.getList.startDates = this.common.getDay(-6); // 开始时间
                this.getList.endDates = this.common.getDay(0); // 截至时间
                this.getList.timeDates = [
                    this.getList.startDates,
                    this.getList.endDates,
                ];
            }
        },

        // 弹框组件
        refreshList() {
            //刷新列表
            this.$nextTick(() => {
                this.yj_searchForm(this.getList);
            });
        },
        closeDialog() {
            this.isShow = false;
            this.isShow1 = false;
        },
        // 弹框组件


        // 筛选条件
        // 时间间隔
        selectTime(e) {
            if (e[0] != "" && e[1] != "") {
                this.getList.startDates = e[0] + " " + "00:00:00"; // 开始时间
                this.getList.endDates = e[1] + " " + "23:59:59"; // 截至时间
            } else {
                this.getList.startDates = ""; // 开始时间
                this.getList.endDates = ""; // 截至时间
            }
            this.getList.timeDates = [e[0], e[1]];
            this.yj_searchForm(this.getList);
        },
        // 筛选条件
    },
};